% Data preparation to generate the figures and run the RDD in COSAR, GRIECO AND TINTELNOT (2014) 
clear
clc

cd 'C:\Dropbox\Research\Finished or retired projects\CGT_WindBorder\RestatFiguresTables\figures'

load('data_structures.mat') % generated by setup.m 
load('est_point.mat')       % estimates, with log specification (\Estimation\Mpec_log_states)

% Distance to border
distance_border            = vertcat(m.dist_bor_ger,m.dist_bor_dnk);
numprojects                = length(distance_border);
numprojects_ger            = length(m.dist_bor_ger);
numprojects_dnk            = length(m.dist_bor_dnk);
country                    = zeros(numprojects,1);
country(1:numprojects_ger) = 1;  
prodc_denmark              = zeros(numprojects,1);

prodc_denmark(1:numprojects_ger) = sum(m.y_matrix_ger(:,2:6)')';
%prodc_denmark(numprojects_ger+1:numprojects) = sum(m.y_matrix_dnk(:,2:6)')';
% to exclude the danish fringe from danish market share, replace above line
% with this one:
prodc_denmark(numprojects_ger+1:numprojects) = sum(m.y_matrix_dnk(:,2:6)')';

% Model fit
rho_ger        = betahat(1:m.num_pro_ger * (m.num_firms_ger + 1) ,1);    
rho_dnk        = betahat(m.num_pro_ger * (m.num_firms_ger + 1)+1 : m.num_pro_ger * (m.num_firms_ger + 1) + m.num_pro_dnk * (m.num_firms_dnk + 1) ,1) ;
rho_ger_mat    = (reshape(rho_ger,m.num_firms_ger + 1,m.num_pro_ger))';
rho_dnk_mat    = (reshape(rho_dnk,m.num_firms_dnk + 1,m.num_pro_dnk))';

den_baseline                                = zeros(numprojects,1);
den_baseline(1:numprojects_ger)             = sum(rho_ger_mat(:,2:6)')';
den_baseline(numprojects_ger+1:numprojects) = sum(rho_dnk_mat(:,2:6)')'; % to exclude the fringe, change index to 2:6
clear rho_ger rho_dnk rho_ger_mat rho_dnk_mat

rho_ger        = e_rho_nF(1:m_nFC.num_pro_ger * (m_nFC.num_firms_ger + 1) ,1);    
rho_dnk        = e_rho_nF(m_nFC.num_pro_ger * (m_nFC.num_firms_ger + 1)+1 : m_nFC.num_pro_ger * (m_nFC.num_firms_ger + 1) + m_nFC.num_pro_dnk * (m_nFC.num_firms_dnk + 1) ,1) ;
rho_ger_mat    = (reshape(rho_ger,m_nFC.num_firms_ger + 1,m_nFC.num_pro_ger))';
rho_dnk_mat    = (reshape(rho_dnk,m_nFC.num_firms_dnk + 1,m_nFC.num_pro_dnk))';

den_nf                                = zeros(numprojects,1);
den_nf(1:numprojects_ger)             = sum(rho_ger_mat(:,2:6)')';
den_nf(numprojects_ger+1:numprojects) = sum(rho_dnk_mat(:,2:6)')';  % to exclude the fringe, change index to 2:6
clear rho_ger rho_dnk rho_ger_mat rho_dnk_mat

rho_ger        = e_rho_nB(1:m_nFC.num_pro_ger * (m_nFC.num_firms_ger + 1) ,1);    
rho_dnk        = e_rho_nB(m_nFC.num_pro_ger * (m_nFC.num_firms_ger + 1)+1 : m_nFC.num_pro_ger * (m_nFC.num_firms_ger + 1) + m_nFC.num_pro_dnk * (m_nFC.num_firms_dnk + 1) ,1) ;
rho_ger_mat    = (reshape(rho_ger,m_nFC.num_firms_ger + 1,m_nFC.num_pro_ger))';
rho_dnk_mat    = (reshape(rho_dnk,m_nFC.num_firms_dnk + 1,m_nFC.num_pro_dnk))';

den_nb                                = zeros(numprojects,1);
den_nb(1:numprojects_ger)             = sum(rho_ger_mat(:,2:6)')';
den_nb(numprojects_ger+1:numprojects) = sum(rho_dnk_mat(:,2:6)')';  % to exclude the fringe, change index to 2:6

data       = [distance_border,country,prodc_denmark,den_baseline,den_nf,den_nb];

save('ModelOutcomes.txt','data','-ascii', '-tabs')

